package jpg1980.gttunes.dao.mysql;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import jpg1980.gttunes.beans.Game;
import jpg1980.gttunes.beans.Manufacturer;
import jpg1980.gttunes.dao.AbstractDao;
import jpg1980.gttunes.dao.GameDao;

public class GameDaoMySqlImpl extends AbstractDao implements GameDao
{

	static Logger logger = Logger.getLogger(GameDaoMySqlImpl.class);
	
	@Override
	public int create(Game game)
	{
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public void delete(int id)
	{
		// TODO Auto-generated method stub

	}

	@Override
	public Game read(int id)
	{
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<Game> readAll()
	{
		List<Game> gameList = new ArrayList<Game>();
		
		PreparedStatement readAllStatement;
		
		try
		{
			Connection conn = getConnection();

			logger.debug("");
			readAllStatement = conn.prepareStatement("SELECT gameName, gameVersion from GAME GROUP BY gameName");
			
			ResultSet rs = readAllStatement.executeQuery();
			
			while (rs.next())
			{
				Game game = new Game(	rs.getString("gameName"),
										rs.getString("gameVersion"));
				
				gameList.add(game);
			}
		}
		catch (SQLException e)
		{
			super.cleanUp();
		}
		finally
		{
			super.cleanUp();
		}
		
		return gameList;
	}

	@Override
	public List<Game> readSelection(int[] ids)
	{
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void update(Game game)
	{
		// TODO Auto-generated method stub

	}

	@Override
	public List<Game> readByGameName(String gameName)
	{
		List<Game> gameList = new ArrayList<Game>();
		
		PreparedStatement readAllStatement;
		
		try
		{
			Connection conn = getConnection();

			logger.debug("");
			readAllStatement = conn.prepareStatement("SELECT gameName, gameVersion from GAME WHERE gameName = ? GROUP BY gameName, gameVersion");
			readAllStatement.setString(1, gameName);
			
			ResultSet rs = readAllStatement.executeQuery();
			
			while (rs.next())
			{
				Game game = new Game(	rs.getString("gameName"),
										rs.getString("gameVersion"));
				
				gameList.add(game);
			}
		}
		catch (SQLException e)
		{
			super.cleanUp();
		}
		finally
		{
			super.cleanUp();
		}
		
		return gameList;
	}

}
